<?php
namespace api\tests;

use Yii;
use api\tests\ApiTester;

class OrdersCest
{
    /** @var token Authorization string **/
    protected $token;

    /** @var user id **/
    protected $uid = '60207942';

    /**
     * 验证授权Authorization
     */
    public function _before(ApiTester $I)
    {
        $this->token = Yii::$app->cache->getOrSet('access_token', function () use ($I) {
            $I->sendGET('/v1/auth', [ 'uid' => $this->uid ]);
            return json_decode($I->grabResponse())->data;
        }, 7200);
        $I->amBearerAuthenticated($this->token->access_token);
    }

    /**
     * 尝试没有access_token访问
     * @throws Authorization 401 error
     */
    public function tryNoTokenToVisit(ApiTester $I)
    {
        $I->haveHttpHeader('Authorization', 'Bearer ' . $this->token->access_token);
        $I->deleteHeader('Authorization');
        $I->sendGET('/v1/shops/3358');
        $I->seeResponseContainsJson(['status' => 401]);
    }

    /**
     * 尝试访问全部订单
     * @throws only access self orders
     */
    public function tryToGetAllTheOrders(ApiTester $I)
    {
        $I->sendGET('/v1/orders');
        $I->seeResponseContainsJson(['success' => true]);
        $bids = $I->grabDataFromResponseByJsonPath('$.data[*].uid');
        $result = yii\helpers\ArrayHelper::getColumn($bids, function ($item) use ($I){
            ($item !== $this->uid) && $I->assertTrue(false);
        });
        sleep(1);
    }

    /**
     * 尝试访问单个订单
     * @throws only access self order
     */
    public function tryToGetASignleOrder(ApiTester $I)
    {
        $I->sendGET('/v1/orders/228204');
        $I->seeResponseContainsJson(['success' => true]);
        $I->seeResponseContainsJson(['id' => 228204, 'uid' => $this->uid]);
        sleep(1);
    }

    /**
     * 更新订单
     * @throws only update self order
     */
    public function tryToUpdateOrder(ApiTester $I)
    {
        $I->sendPATCH('/v1/orders/228204', [
            'numberno' => '98888888',
        ]);
        $I->seeResponseContainsJson(['success' => true]);
        $I->seeResponseContainsJson(['uid' => $this->uid, 'numberno' => '98888888']);
        sleep(1);
    }

    /**
     * 尝试创建订单
     * @throws only create self order.
     */
    public function tryToCreateOrder(ApiTester $I)
    {
    }

    /**
     * 尝试删除订单
     * @throws cannot be delete
     */
    public function tryToDeleteOrder(ApiTester $I)
    {
    }
}
